// pages/plugins/exam/details/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    showTips: true,
    // 倒计时
    examTimer: '',
    // 考试id
    id: null,
    // 剩余时间
    remainingTime: 0,
    // 页码
    page: 1,
    // 题目列表
    questionList: [],
    // 临时答案
    answerTemp: {},
    // 是否限制粘贴
    canPaste: 0,
  },

  hiddenDialog(){
    this.setData({
      showTips: false
    });
    this.setExamTimer(this.data.surplusTime);
    this.loadExamPaper();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id || null,
      canHideWxapp: options.canHideWxapp || 0,
      hideWxappCount: options.hideWxappCount || 0,
      canPaste: options.canPaste,
      surplusTime: options.time
    });
  },

  // 设置定时器
  setExamTimer(timeLength) {
    let that = this;
    that.data.examTimer = setInterval(() => {
      wx.setNavigationBarTitle({
        title: '剩余时间：' + app.utils.formatSecond(timeLength--),
      })
      that.setData({
        remainingTime: timeLength
      })
      if (timeLength === 0) {
        clearInterval(that.data.examTimer);
        app.showConfirm('考试结束', '考试时间到，已自动为您提交~', false, function() {
          // that.formSubmit();
          wx.redirectTo({
            url: '/pages/agent/plugins/exam/result/index?id='+that.data.id,
          })
        })
      } else if (timeLength === 300) {
        wx.showToast({
          image: '/images/agent/center/index/07.png',
          title: '还有5分钟~',
        })
      }
    }, 1000);
  },

  // 获取考试题目
  loadExamPaper() {
    var that = this;
    var params = {
      point_url: app.server.agentApi.getExamQuestionList,
      id: that.data.id,
      pageNo: that.data.page++,
      pageSize: 10
    }
    app.server.getConnect(params).then(function(res) {
      that.setData({
        questionList: that.data.questionList.concat(res.data.list)
      })
    })
  },

  // 题选择
  bindQuestionChange(e){
    console.log(e);
    let value = e.detail.value;
    value instanceof Array && (value = value.join(','));
    this.data.answerTemp[e.currentTarget.id]=value;
    console.log(this.data.answerTemp)
  },

  // 预提交答卷
  previewFormSubmit: function(e) {
    var that = this;
    var answer = [];
    for (var key in this.data.answerTemp){
      answer.push({ 'key': key, 'answer': this.data.answerTemp[key]})
    }
    var params = {
      point_url: app.server.agentApi.submitExamPreview,
      id: that.data.id,
      answer: JSON.stringify(answer)
    }
    app.server.getConnect(params).then(function(res) {
      // app.showConfirm('提示', '考试时间剩余' + app.utils.formatSecond(res.data.surplus_time) + (res.data.number > 0 ? (', 您还有' + res.data.number + '道题未做') : '') + ',是否立即提交~', true, function() {
      //   // that.formSubmit();
      //   // console.log(res)
      // });
      app.showConfirm('提示', '考试时间剩余' + res.data.surplus_time + (res.data.number > 0 ? (', 您还有' + res.data.number + '道题未做') : '') + ',是否立即提交~', true, function () {
        wx.redirectTo({
          url: '/pages/agent/plugins/exam/result/index?id=' + that.data.id,
        })
      });
    })
  },

  // 提交答卷
  formSubmit(){
    var that = this;
    var answer = [];
    for (var key in this.data.answerTemp) {
      answer.push({ 'key': key, 'answer': this.data.answerTemp[key] })
    }
    var params = {
      point_url: app.server.agentApi.submitExam,
      id: that.data.id,
      answer: JSON.stringify(answer)
    }
    app.server.getConnect(params).then(function (res) {
      clearInterval(that.data.examTimer);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 清除剪切板内容
    if (this.data.canPaste){
      wx.getClipboardData({
        success(res) {
          if(res.data){
            wx.setClipboardData({ data: '' });
          }
        }
      })
    }
  },

  onReachBottom: function() {
    this.loadExamPaper();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearInterval(this.data.examTimer);
    this.formSubmit();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
